package cn.iocoder.yudao.module.member.service.memberconf.confpoint;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.confpoint.vo.MemberConfPointPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.confpoint.vo.MemberConfPointSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confpoint.MemberConfPointDO;
import cn.iocoder.yudao.module.member.dal.mysql.memberconf.confpoint.MemberConfPointMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.CONF_POINT_NOT_EXISTS;

/**
 * 积分奖励规则 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MemberConfPointServiceImpl implements MemberConfPointService {

    @Resource
    private MemberConfPointMapper confPointMapper;

    @Override
    public Long createConfPoint(MemberConfPointSaveReqVO createReqVO) {
        // 插入
        MemberConfPointDO confPoint = BeanUtils.toBean(createReqVO, MemberConfPointDO.class);
        confPointMapper.insert(confPoint);
        // 返回
        return confPoint.getId();
    }

    @Override
    public void updateConfPoint(MemberConfPointSaveReqVO updateReqVO) {
        // 校验存在
        validateConfPointExists(updateReqVO.getId());
        // 更新
        MemberConfPointDO updateObj = BeanUtils.toBean(updateReqVO, MemberConfPointDO.class);
        confPointMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfPoint(Long id) {
        // 校验存在
        validateConfPointExists(id);
        // 删除
        confPointMapper.deleteById(id);
    }

    private void validateConfPointExists(Long id) {
        if (confPointMapper.selectById(id) == null) {
            throw exception(CONF_POINT_NOT_EXISTS);
        }
    }

    @Override
    public MemberConfPointDO getConfPoint(Long id) {
        return confPointMapper.selectById(id);
    }

    @Override
    public PageResult<MemberConfPointDO> getConfPointPage(MemberConfPointPageReqVO pageReqVO) {
        return confPointMapper.selectPage(pageReqVO);
    }

}